<template>
  <el-dialog
    v-model="dialogVisible"
    title="查询生产订单"
    width="900px"
    @close="closeDialog"
  >
    <el-form class="aceauForm" label-width="120px" :model="state.form">
      <el-row :gutter="12">
        <el-col :span="12">
          <el-form-item label="生产订单号">
            <el-input v-model="state.form.scddh" placeholder="" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="订单类型" required>
            <el-select
              v-model="state.form.pkMorderType"
              placeholder="类型"
              style="width: 100%"
            >
              <el-option
                v-for="moType in moTypes"
                :key="moType.pkMorderType"
                :label="moType.typeName"
                :value="moType.pkMorderType"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="存货编码" required>
            <el-input
              v-model="state.form.invcode"
              placeholder=""
              :suffix-icon="Search"
              @click="toSelectInv"
              @focus="toSelectInv"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="存货名称">
            <el-input v-model="state.form.invname" disabled placeholder="" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="存货规格">
            <el-input v-model="state.form.invspec" disabled placeholder="" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="存货型号">
            <el-input v-model="state.form.invtype" disabled placeholder="" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工作中心" required>
            <el-select
              v-model="state.form.pkWc"
              placeholder="中心"
              style="width: 100%"
            >
              <el-option
                v-for="wc in wcs"
                :key="wc.pkWc"
                :label="wc.wcName"
                :value="wc.pkWc"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="query">查询</el-button>
      </span>
    </template>
  </el-dialog>
  <invSelect ref="invSelectRef" @selected-callback="invSelectCallback" />
</template>

<script setup>
  import invSelect from '@/plugins/InventorySelector/invSelect.vue'
  import { localStorageConfig } from '@/config/localstorage.config'
  import { getWcListApi } from '@/api/base/wc/index'
  import { getMesMorderTypesApi } from '@/api/mes/morder_api'

  // const $baseLoading = inject('$baseLoading')
  // const $baseMessage = inject('$baseMessage')

  const pkCorp = localStorage.getItem(localStorageConfig.__pkCorp)
  const dialogVisible = ref(false)

  const state = reactive({
    form: {
      pkCorp: pkCorp,
    },
  })

  const moTypes = ref()
  const invSelectRef = ref()
  const wcs = ref()

  const openDialog = () => {
    getMesMorderTypes()
    getWcList()
    dialogVisible.value = true
  }

  const getWcList = async () => {
    let con = { pkCorp: pkCorp }
    const res = await getWcListApi(con)
    if (res.code === 2000) {
      wcs.value = res.data
    }
  }

  const getMesMorderTypes = async () => {
    const res = await getMesMorderTypesApi()
    if (res.code === 2000) {
      state.form.pkMorderType = res.data[0].pkMorderType
      moTypes.value = res.data
    }
  }

  const toSelectInv = () => {
    let params = {
      isSelf: 1,
    }
    invSelectRef.value.handleOpen(params, 1)
  }
  const invSelectCallback = (selectedInv) => {
    state.form.pkInv = selectedInv.pkInv
    state.form.invcode = selectedInv.invcode
    state.form.invname = selectedInv.invname
    state.form.invspec = selectedInv.invspec
    state.form.invtype = selectedInv.invtype
    state.form.measName = selectedInv.measName
  }

  const query = () => {
    dialogVisible.value = false
    _emit('success-callback', state.form)
  }

  defineExpose({ openDialog })
  const _emit = defineEmits(['success-callback'])
</script>
